package safeCommitter.core.view.main.progress;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import safeCommitter.core.view.main.SCViewUtils;
import safeCommitter.test.executor.common.TestResultType;

public class CounterPanel extends Composite {
	private static final String TOTAL_MESSAGE_FORMAT = "{0}/{1}  ";
	private final Image m_successIcon = SCViewUtils
			.createImage("icons/success.gif");
	private final Image m_failureIcon = SCViewUtils
			.createImage("icons/failure.gif");
	private final Image m_skipIcon = SCViewUtils.createImage("icons/skip.gif");
	// private final Image m_failPercentIcon =
	// TestNGPlugin.getImageDescriptor("ovr16/failureOnPercentage.gif").createImage();
	// //$NON-NLS-1$

	// protected Text m_suiteCountText;
	// protected Text m_testCountText;
	// protected Text m_methodCountText;

	protected Text m_passedText;
	protected CLabel m_passedLabel;
	protected Text m_skippedText;
	protected CLabel m_skippedLabel;
	protected Text m_failedText;
	protected CLabel m_failedLabel;
	// protected Text m_successPercentageFailedText;

	protected int m_suiteTotalCount;
	protected int m_suiteCount = 0;
	protected int m_testTotalCount;
	protected int m_testCount = 0;
	protected int m_methodTotalCount;
	protected int m_methodCount = 0;

	private int skipCount = 0;
	private int failCount = 0;
	private int successCount = 0;

	public CounterPanel(final Composite parent) {
		super(parent, SWT.WRAP);

		// FillLayout fl = new FillLayout(SWT.VERTICAL);
		// fl.marginWidth = 0;
		// fl.marginHeight = 0;
		GridLayout gl = new GridLayout(1, false);
		gl.marginWidth = 0;
		gl.marginHeight = 0;
		GridData gd = new GridData(GridData.FILL_BOTH);
		setLayout(gl);
		setLayoutData(gd);

		createReportUpperRow();

		// createReportLowerRow();

		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(final DisposeEvent e) {
				disposeIcons();
			}
		});
	}

	private void disposeIcons() {
		m_successIcon.dispose();
		m_failureIcon.dispose();
		m_skipIcon.dispose();
		// m_failPercentIcon.dispose();
	}

	private void createReportUpperRow() {
		Composite upperRow = new Composite(this, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 5;
		upperRow.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		upperRow.setLayout(gridLayout);

		Color backgroundColor = getDisplay().getSystemColor(
				SWT.COLOR_WIDGET_BACKGROUND);

		// m_suiteCountText = createLabel(upperRow, "Suites:", null,
		// "0/0 ", backgroundColor);
		// m_testCountText = createLabel(upperRow, "Tests:", null,
		// "0/0 ", backgroundColor);
		// m_methodCountText = createLabel(upperRow, "Methods:", null,
		// "0/0 ", backgroundColor);
		// m_passedText = createLabel(upperRow, "Passed:", m_successIcon, " 0 ",
		// backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$
		// m_failedText = createLabel(upperRow, "Failed:", m_failureIcon, " 0 ",
		// backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$
		// m_skippedText = createLabel(upperRow, "Skipped:", m_skipIcon, " 0 ",
		// backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$
		m_passedLabel = createLabel(upperRow,
				"Passed: 0", m_successIcon, "  0  ", backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$
		m_failedLabel = createLabel(upperRow,
				"Failed: 0", m_failureIcon, "  0  ", backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$
		m_skippedLabel = createLabel(upperRow,
				"Skipped: 0", m_skipIcon, "  0  ", backgroundColor); //$NON-NLS-1$ //$NON-NLS-2$

	}

	private CLabel createLabel(final Composite parent, final String name,
			final Image image, final String init, final Color backColor) {
		// Label label = null;
		// if(image != null) {
		// label= new Label(parent, SWT.NONE);
		// image.setBackground(label.getBackground());
		// label.setImage(image);
		// label.setLayoutData(new
		// GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
		// }

		CLabel label = new CLabel(parent, SWT.NONE);
		if (null != image) {
			label.setImage(image);
		}
		label.setText(name);
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
				| GridData.FILL_HORIZONTAL));
		label.setFont(JFaceResources.getDialogFont());

		// Text value = new Text(parent, SWT.READ_ONLY);
		// value.setText(init);
		// value.setBackground(backColor);
		// value.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
		// | GridData.HORIZONTAL_ALIGN_BEGINNING));
		//
		// return value;
		return label;
	}

	/**
	 * FIXME: too much redraw().
	 */
	public void reset() {
		ppp("reset()");
		m_suiteTotalCount = 0;
		m_suiteCount = 0;
		m_testTotalCount = 0;
		m_testCount = 0;
		m_methodTotalCount = 0;
		m_methodCount = 0;
		successCount = 0;
		failCount = 0;
		skipCount = 0;
		// setSuccessPercentageFailedCount(0);
		redraw();
	}

	public void incPassedCount() {
		successCount++;
		m_passedLabel.setText("Passed: " + successCount);
		m_passedLabel.redraw();
	}

	public void incFailedCount() {
		failCount++;
		m_failedLabel.setText("Failed: " + failCount);
		m_failedLabel.redraw();
	}

	public void incSkippedCount() {
		skipCount++;
		m_skippedLabel.setText("Skipped: " + skipCount);
		m_skippedLabel.redraw();
	}

	/*
	 * public void setSuccessPercentageFailedCount(int no) {
	 * m_successPercentageFailedText.setText(String.valueOf(no));
	 * m_successPercentageFailedText.redraw(); redraw(); }
	 */

	private static void ppp(final Object msg) {
		// System.out.println("[CounterPanel]:- " + msg);
	}

	public void addResult(final TestResultType status) {
		if (TestResultType.SUCCESS.equals(status)) {
			incPassedCount();
		} else if (TestResultType.SKIPPED.equals(status)) {
			incSkippedCount();
		} else {
			incFailedCount();
		}

	}
}
